using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.VisualStudio.Modeling;
using EastBancTech.DSL.FileSystemDefinition;

namespace EastBancTech.InstallerDefinition
{
    public partial class ComponentFileSource
    {
        protected virtual String GetFileModelName()
        {
            return new Guid().ToString();
        }

        public EastBancTech.DSL.FileSystemDefinition.FileSystemModel FileSystem
        {
            get
            {
                EastBancTech.DSL.FileSystemDefinition.FileSystemModel model = null;

                if (this.Store.FindDomainModel(EastBancTech.DSL.FileSystemDefinition.FileSystemDefinitionDomainModel.DomainModelId) == null)
                {
                    try
                    {
                        Store.TransactionManager.BeginTransaction("Load FileSystem Model");
                        this.Store.LoadDomainModels(new Type[] { typeof(EastBancTech.DSL.FileSystemDefinition.FileSystemDefinitionDomainModel) });
                        Store.TransactionManager.CurrentTransaction.Commit();
                    }
                    finally
                    {
                        if (Store.TransactionManager.CurrentTransaction != null)
                            Store.TransactionManager.CurrentTransaction.Rollback();
                    }
                }

                try
                {
                    if (!this.Store.PropertyBag.ContainsKey(this.Id))
                    {

                        Store.TransactionManager.BeginTransaction("Create FileSystem Model FileSystem Model");

                        Partition part = new Partition(Store);
                        part.Id = Guid.NewGuid();
                        model = new EastBancTech.DSL.FileSystemDefinition.FileSystemModel(part);
                        model.Name = GetFileModelName();

                        BuildFileSystemModel(model);

                        SetWixSpecificProperties(model);

                        Store.TransactionManager.CurrentTransaction.Commit();

                        this.Store.PropertyBag[this.Id] = model;
                    }
                    else
                        model = this.Store.PropertyBag[this.Id] as EastBancTech.DSL.FileSystemDefinition.FileSystemModel;
                }
                finally
                {
                    if (Store.TransactionManager.CurrentTransaction != null)
                        Store.TransactionManager.CurrentTransaction.Rollback();
                }


                return model;
            }
        }

        protected virtual void SetWixSpecificProperties(EastBancTech.DSL.FileSystemDefinition.FileSystemModel model)
        {
            foreach (FileModel file in this.Store.ElementDirectory.FindElements(FileModel.DomainClassId, true))
            {
                file.Properties.Add(new EastBancTech.DSL.FileSystemDefinition.Property(
                    this.Store,
                    new PropertyAssignment(Property.NameDomainPropertyId, "Permanent"),
                    new PropertyAssignment(Property.ValueDomainPropertyId, IsFilePermanent(file.Path).ToString())));

                if (file.GetDomainClass().Id.Equals(AssemblyFileModel.DomainClassId))
                {
                    file.Properties.Add(new EastBancTech.DSL.FileSystemDefinition.Property(
                    this.Store,
                    new PropertyAssignment(Property.NameDomainPropertyId, "InstallUtil"),
                    new PropertyAssignment(Property.ValueDomainPropertyId, IsFileInstallUtil(file.Path).ToString())));

                    file.Properties.Add(new EastBancTech.DSL.FileSystemDefinition.Property(
                       this.Store,
                       new PropertyAssignment(Property.NameDomainPropertyId, "Gac"),
                       new PropertyAssignment(Property.ValueDomainPropertyId, IsFileGac(file.Path).ToString())));
                }
            }
        }

        protected virtual void BuildFileSystemModel(EastBancTech.DSL.FileSystemDefinition.FileSystemModel fileSystemModel)
        {

        }

        public virtual bool IsFilePermanent(String fileRelativePath)
        {
            return this.PermanentFileSet != null ? this.PermanentFileSet.IsFileIncluded(fileRelativePath) : false;
        }

        public virtual bool IsFileInstallUtil(String fileRelativePath)
        {
            return this.InstallUtilFileSet != null ? this.InstallUtilFileSet.IsFileIncluded(fileRelativePath) : false;
        }

        public virtual bool IsFileGac(String fileRelativePath)
        {
            return this.GacFileSet != null ? this.GacFileSet.IsFileIncluded(fileRelativePath) : false;
        }
    }
}
